package cn.uexpo.lhz.common.oldUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class StringDate {

	
	
	/**
	* JAVA获得0-9,a-z,A-Z范围的随机数
	* @param length 随机数长度
	* @return String
	*/

	public static String getRandomChar(int length) {
		char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
	
		Random random = new Random();
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < length; i++) {
		buffer.append(chr[random.nextInt(62)]);
		}
	return buffer.toString();
	}

	/**
	 * 获取当前日期字符串：年月日时分秒
	 * @return
	 */
	public static String getNowDateStr() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	
	
	public static Date getNow() {
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now=new Date();
		String currentStr=sdf.format(now);
		
//		System.out.println("---currentStr------"+currentStr);
		try {
			now=sdf.parse(currentStr);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return now;
	}
	public static String getNowTimeStr() {
		SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMddHHmmss");
		Date now=new Date();
		String currentStr=sdf.format(now);
		return currentStr;
	}
	public static String timeStamp2Date(Long timeLong) {
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
	    Date date;
	    try {
	        date = sdf.parse(sdf.format(timeLong));
	        return sdf.format(date);
	    } catch (Exception e) {
	        e.printStackTrace();
	        return null;
	    }
	}  
	
	
	public static String DateToString(Date date) {
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
	    try {
	        return sdf.format(date);
	    } catch (Exception e) {
	        e.printStackTrace();
	        return null;
	    }
	}  
	
	
	public static Date getDateTime1(String currentStr) {
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date time=null;
//		System.out.println("---currentStr------"+currentStr);
		try {
			time=sdf.parse(currentStr);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return time;
	}
	public static Date getDateYMD(String currentStr) {
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		Date time=null;
		System.out.println("---currentStr------"+currentStr);
		try {
			time=sdf.parse(currentStr);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return time;
	}
	public static Date getDateHMS(String currentStr) {
		SimpleDateFormat sdf =new SimpleDateFormat("HH:mm:ss");
//		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date time=null;
		System.out.println("---currentStr------"+currentStr);
		try {
			time=sdf.parse(currentStr);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return time;
	}

	//秒转换
	public static String secondToTime(long m) {
		if(m < 60) {//秒
			return NumFormat(0) + ":" + NumFormat(m)+ "秒";
		}
		if(m < 3600) {//分
			return NumFormat(m / 60) + "分" + NumFormat(m % 60)+ "秒";
		}
		if(m < 3600 * 24) {//时
			return NumFormat(m / 60 / 60) + "时" + NumFormat(m / 60 % 60) + "分" + NumFormat(m % 60)+ "秒";
		}
		if(m >= 3600 * 24) {//天
			return NumFormat(m / 60 / 60 /24) + "天" +NumFormat(m / 60 / 60 % 24) + "时" + NumFormat(m / 60 % 60) + "分" + NumFormat(m % 60)+ "秒";
		}
		return "--";
	}

	public static String NumFormat(long i) {
		if(String.valueOf(i).length() < 2) {
			return "0"+i;
		}else {
			return String.valueOf(i);
		}
	}

	public static void main(String[] args) {
		System.out.println(StringDate.getNow());
		System.out.println(getRandomChar(4));
		System.out.println("---getNowDateStr:"+getNowDateStr());

		System.out.println("---ssssssssss:"+secondToTime(2099));
		
	}

}
